﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductNoteBL
    {
        public void Insert(tbl_ProductNote productNote)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductNote newproductNote = new tbl_ProductNote()
                    {
                        ToDate = productNote.ToDate,
                        Status = productNote.Status,
                        Note = productNote.Note,     
                        FromDate = productNote.FromDate,
                        ProductNo=productNote.ProductNo,
                        CreatedUser = productNote.CreatedUser,
                        CreatedDate = productNote.CreatedDate
                    };
                    contextdb.tbl_ProductNotes.InsertOnSubmit(newproductNote);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductNote productNote)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductNote = contextdb.tbl_ProductNotes.SingleOrDefault(u => u.LogId == productNote.LogId);
                try
                {
                    if (updateproductNote != null)
                    {
                        updateproductNote.ToDate = productNote.ToDate;
                        updateproductNote.Status = productNote.Status;
                        updateproductNote.ProductNo = productNote.ProductNo;
                        updateproductNote.Note = productNote.Note;
                        updateproductNote.FromDate = productNote.FromDate;
                        updateproductNote.ProductNo = productNote.ProductNo;
                        updateproductNote.CreatedUser = productNote.CreatedUser;
                        updateproductNote.CreatedDate = productNote.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int logId)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteRelateNo = contextdb.tbl_ProductNotes.SingleOrDefault(u => u.LogId == logId);
                try
                {
                    if (deleteRelateNo != null)
                    {
                        contextdb.tbl_ProductNotes.DeleteOnSubmit(deleteRelateNo);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int logId)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteRelateNo = contextdb.tbl_ProductNotes.SingleOrDefault(u => u.LogId == logId);
                try
                {
                    if (deleteRelateNo != null)
                    {
                        deleteRelateNo.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
